package jxau.jwgl.utils.net;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

/**
 * RetrofitBuilder
 *
 * @author LongShu 2017/04/03
 */
public class RetrofitBuilder {

    private String baseUrl;

    public RetrofitBuilder() {
    }

    public RetrofitBuilder(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public Retrofit.Builder builder() {
        Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
                //.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(baseUrl);

        return retrofitBuilder;
    }

    public Retrofit.Builder builder(OkHttpClient client) {
        return builder().client(client);
    }

    /**
     * 创建常规类型的webApi
     *
     * @param webApi 包含retrofit注解的接口
     * @return
     */
    public static <T> T create(Retrofit.Builder builder, Class<T> webApi) {
        checkNotNull(webApi, "webApi==null");
        return builder.build().create(webApi);
    }

    static <T> T checkNotNull(T object, String message) {
        if (object == null) {
            throw new NullPointerException(message);
        }
        return object;
    }

    public String getBaseUrl() {
        return baseUrl;
    }

    public RetrofitBuilder setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
        return this;
    }

}
